package pdf;
import Controlador.Controlador;
import Definiciones.Definiciones;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.Date;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *
 * @author leynar
 */
public class pdf {

    //Contiene la ruta donde se creará el pdf
    private String _path;
    //Contiene el documento
    private Document _document;
    private String _name;

    public pdf(String path, String nombre_informe) throws FileNotFoundException, BadElementException, MalformedURLException, IOException, DocumentException {
        this._path = path;
        this._document = new Document(PageSize.A4);
        this._name = nombre_informe;
        

    }
   /* public static void main(String arg[]) throws FileNotFoundException, BadElementException, MalformedURLException, IOException, DocumentException {
        String [][] tabla = new String [200][3];

        for(int i = 0; i < 200; i++) {
            for(int j = 0; j < 3; j++) {
                tabla[i][j] = "hola";
            }
        }

        pdf pdf = new pdf("/Users/leynar/Desktop/prueba.pdf", "Informe stock total");
        pdf.pdfInformeTabla(tabla);
    }*/

    public void pdfInformeTabla(String tabla[][]) {
         try {
            //Document documento = this._document;
            Image image = Image.getInstance(getClass().getResource("/clinica/resources/logo.png"));
            OutputStream file = new FileOutputStream(new File(this._path));
            PdfWriter.getInstance(this._document, file);
            this._document.open();
            image.scaleAbsolute(150, 150);
            Calendar c = Calendar.getInstance();
            String fecha = Controlador.mostrarFecha(c.getTime());
            Paragraph p = new Paragraph("Fecha: "+fecha.substring(4, fecha.length()));
            p.setAlignment(Paragraph.ALIGN_RIGHT);
            this._document.add(p);
            this._document.add(image);
            p = new Paragraph(" "+this._name);
            
            p.setAlignment(Paragraph.ALIGN_CENTER);
            this._document.add(p);

            PdfPTable table=new PdfPTable(tabla[0].length);
            table.setSpacingBefore(50);
            for(int i=0; i < tabla.length; i++)
                for(int j = 0; j < tabla[i].length; j++)
                    table.addCell(tabla[i][j]);
            this._document.add(table);

            this._document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    
    }



}
